package com.example2016.cjj.servicedemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    private Intent serviceIntent;
    private ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        serviceIntent = new Intent(this,MyService.class);

        Button startServiceBtn = (Button) findViewById(R.id.btn_startService);
        Button stopServiceBtn = (Button) findViewById(R.id.btn_stopService);
        Button bindServiceBtn = (Button) findViewById(R.id.btn_bindService);
        Button unbindServiceBtn = (Button) findViewById(R.id.btn_unbindService);
        Button startIntentServiceBtn = (Button) findViewById(R.id.btn_startIntentService);
        startServiceBtn.setOnClickListener(this);
        stopServiceBtn.setOnClickListener(this);
        bindServiceBtn.setOnClickListener(this);
        unbindServiceBtn.setOnClickListener(this);
        startIntentServiceBtn.setOnClickListener(this);
        Button startDownloadActivityBtn = (Button) findViewById(R.id.btn_startDownloadActivity);
        startDownloadActivityBtn.setOnClickListener(this);



        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "onServiceConnected: execute");
                MyService.DownloadBinder downloadBinder = (MyService.DownloadBinder)service;
                downloadBinder.startDownload();
                downloadBinder.getProgress();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "onServiceDisconnected: execute");
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_startService:

                startService(serviceIntent);  //启动服务
                break;
            case R.id.btn_stopService:

                stopService(serviceIntent);  //停止服务
                break;
            case R.id.btn_bindService:
                bindService(serviceIntent,serviceConnection,BIND_AUTO_CREATE);  //绑定服务
                break;
            case R.id.btn_unbindService:
                unbindService(serviceConnection); //解除绑定服务
                break;
            case R.id.btn_startIntentService:
                Log.d(TAG, "Thread id is "+ Thread.currentThread().getId());
                Intent intentService = new Intent(this, MyIntentService.class);
                startService(intentService);
                break;
            case R.id.btn_startDownloadActivity:
                Intent downloadIntent = new Intent(this,DownloadActivity.class);
                startActivity(downloadIntent);
                break;
            default:
                break;
        }
    }
}
